
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 语言切片(Slice) · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../part16/" />
    
    
    <link rel="prev" href="../part14/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../part9/">
            
                <a href="../part9/">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../part10/">
            
                <a href="../part10/">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../part11/">
            
                <a href="../part11/">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../part13/">
            
                <a href="../part13/">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../part14/">
            
                <a href="../part14/">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.16" data-path="./">
            
                <a href="./">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../part16/">
            
                <a href="../part16/">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../part17/">
            
                <a href="../part17/">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../part20/">
            
                <a href="../part20/">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../part22/">
            
                <a href="../part22/">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 语言切片(Slice)</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x8BED;&#x8A00;&#x5207;&#x7247;slice">Go &#x8BED;&#x8A00;&#x5207;&#x7247;(Slice)</h1>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x5207;&#x7247;&#x662F;&#x5BF9;&#x6570;&#x7EC4;&#x7684;&#x62BD;&#x8C61;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x6570;&#x7EC4;&#x7684;&#x957F;&#x5EA6;&#x4E0D;&#x53EF;&#x6539;&#x53D8;&#xFF0C;&#x5728;&#x7279;&#x5B9A;&#x573A;&#x666F;&#x4E2D;&#x8FD9;&#x6837;&#x7684;&#x96C6;&#x5408;&#x5C31;&#x4E0D;&#x592A;&#x9002;&#x7528;&#xFF0C;Go&#x4E2D;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x7075;&#x6D3B;&#xFF0C;&#x529F;&#x80FD;&#x5F3A;&#x608D;&#x7684;&#x5185;&#x7F6E;&#x7C7B;&#x578B;<code>&#x5207;&#x7247;</code>(&quot;&#x52A8;&#x6001;&#x6570;&#x7EC4;&quot;)&#xFF0C;&#x4E0E;&#x6570;&#x7EC4;&#x76F8;&#x6BD4;&#x5207;&#x7247;&#x7684;&#x957F;&#x5EA6;&#x662F;&#x4E0D;&#x56FA;&#x5B9A;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x8FFD;&#x52A0;&#x5143;&#x7D20;&#xFF0C;&#x5728;&#x8FFD;&#x52A0;&#x65F6;&#x53EF;&#x80FD;&#x4F7F;&#x5207;&#x7247;&#x7684;&#x5BB9;&#x91CF;&#x589E;&#x5927;&#x3002;</p>
<hr>
<h2 id="&#x5B9A;&#x4E49;&#x5207;&#x7247;">&#x5B9A;&#x4E49;&#x5207;&#x7247;</h2>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x672A;&#x6307;&#x5B9A;&#x5927;&#x5C0F;&#x7684;&#x6570;&#x7EC4;&#x6765;&#x5B9A;&#x4E49;&#x5207;&#x7247;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">var</span> identifier []<span class="hljs-keyword">type</span>
</code></pre>
<p>&#x5207;&#x7247;&#x4E0D;&#x9700;&#x8981;&#x8BF4;&#x660E;&#x957F;&#x5EA6;&#x3002;
&#x6216;&#x4F7F;&#x7528;make()&#x51FD;&#x6570;&#x6765;&#x521B;&#x5EFA;&#x5207;&#x7247;:</p>
<pre><code class="lang-go"><span class="hljs-keyword">var</span> slice1 []<span class="hljs-keyword">type</span> = <span class="hljs-built_in">make</span>([]<span class="hljs-keyword">type</span>, <span class="hljs-built_in">len</span>)
<span class="hljs-comment">// &#x4E5F;&#x53EF;&#x4EE5;&#x7B80;&#x5199;&#x4E3A;</span>
<span class="hljs-comment">// slice1 := make([]type, len)</span>
</code></pre>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x5BB9;&#x91CF;&#xFF0C;&#x5176;&#x4E2D;capacity&#x4E3A;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-built_in">make</span>([]T, length, capacity)
</code></pre>
<p>&#x8FD9;&#x91CC; len &#x662F;&#x6570;&#x7EC4;&#x7684;&#x957F;&#x5EA6;&#x5E76;&#x4E14;&#x4E5F;&#x662F;&#x5207;&#x7247;&#x7684;&#x521D;&#x59CB;&#x957F;&#x5EA6;&#x3002;</p>
<h3 id="&#x5207;&#x7247;&#x521D;&#x59CB;&#x5316;">&#x5207;&#x7247;&#x521D;&#x59CB;&#x5316;</h3>
<pre><code class="lang-go">s := [] <span class="hljs-keyword">int</span> {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>}
</code></pre>
<p>&#x76F4;&#x63A5;&#x521D;&#x59CB;&#x5316;&#x5207;&#x7247;&#xFF0C;<code>[]</code>&#x8868;&#x793A;&#x662F;&#x5207;&#x7247;&#x7C7B;&#x578B;&#xFF0C;<code>{1, 2, 3}</code>&#x521D;&#x59CB;&#x5316;&#x503C;&#x4F9D;&#x6B21;&#x662F;<code>1, 2, 3</code>&#xFF0C;&#x5176;<code>cap=len=3</code></p>
<pre><code class="lang-go">s := arr[:]
</code></pre>
<p>&#x521D;&#x59CB;&#x5316;&#x5207;&#x7247;<code>s</code>,&#x662F;&#x6570;&#x7EC4;<code>arr</code>&#x7684;&#x5F15;&#x7528;</p>
<pre><code class="lang-go">s := arr[startIndex:endIndex]
</code></pre>
<p>&#x5C06;arr&#x4E2D;&#x4ECE;&#x4E0B;&#x6807;<code>startIndex</code>&#x5230;<code>endIndex-1</code> &#x4E0B;&#x7684;&#x5143;&#x7D20;&#x521B;&#x5EFA;&#x4E3A;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5207;&#x7247;</p>
<pre><code class="lang-go">s := arr[startIndex:]
</code></pre>
<p>&#x9ED8;&#x8BA4; <code>endIndex</code> &#x65F6;&#x5C06;&#x8868;&#x793A;&#x4E00;&#x76F4;&#x5230;arr&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;</p>
<pre><code class="lang-go">s := arr[:endIndex]
</code></pre>
<p>&#x9ED8;&#x8BA4; <code>startIndex</code> &#x65F6;&#x5C06;&#x8868;&#x793A;&#x4ECE;<code>arr</code>&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x5F00;&#x59CB;</p>
<pre><code class="lang-go">s1 := s[startIndex:endIndex]
</code></pre>
<p>&#x901A;&#x8FC7;&#x5207;&#x7247;s&#x521D;&#x59CB;&#x5316;&#x5207;&#x7247;s1</p>
<pre><code class="lang-go">s :=<span class="hljs-built_in">make</span>([]<span class="hljs-keyword">int</span>,<span class="hljs-built_in">len</span>,<span class="hljs-built_in">cap</span>)
</code></pre>
<p>&#x901A;&#x8FC7;&#x5185;&#x7F6E;&#x51FD;&#x6570;<code>make()</code>&#x521D;&#x59CB;&#x5316;&#x5207;&#x7247;<code>s</code>&#xFF0C;<code>[]int</code> &#x6807;&#x8BC6;&#x4E3A;&#x5176;&#x5143;&#x7D20;&#x7C7B;&#x578B;&#x4E3A;<code>int</code>&#x7684;&#x5207;&#x7247;</p>
<hr>
<h2 id="len-&#x548C;-cap-&#x51FD;&#x6570;">len() &#x548C; cap() &#x51FD;&#x6570;</h2>
<p>&#x5207;&#x7247;&#x662F;&#x53EF;&#x7D22;&#x5F15;&#x7684;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x7531; <code>len()</code> &#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x957F;&#x5EA6;&#x3002;</p>
<p>&#x5207;&#x7247;&#x63D0;&#x4F9B;&#x4E86;&#x8BA1;&#x7B97;&#x5BB9;&#x91CF;&#x7684;&#x65B9;&#x6CD5; <code>cap()</code> &#x53EF;&#x4EE5;&#x6D4B;&#x91CF;&#x5207;&#x7247;&#x6700;&#x957F;&#x53EF;&#x4EE5;&#x8FBE;&#x5230;&#x591A;&#x5C11;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x4E3A;&#x5177;&#x4F53;&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> numbers = <span class="hljs-built_in">make</span>([] <span class="hljs-keyword">int</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>)

    printSlice(numbers)
}


<span class="hljs-keyword">func</span> printSlice(x [] <span class="hljs-keyword">int</span>) {
    t.Printf(<span class="hljs-string">&quot;len=%d, cap=%d, slice=%v\n&quot;</span>, <span class="hljs-built_in">len</span>(x), <span class="hljs-built_in">cap</span>(x), x)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8FD0;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;:</p>
<pre><code class="lang-sh">len=3, <span class="hljs-built_in">cap</span>=5, slice=[0 0 0]
</code></pre>
<hr>
<h2 id="&#x7A7A;nil&#x5207;&#x7247;">&#x7A7A;(nil)&#x5207;&#x7247;</h2>
<p>&#x4E00;&#x4E2A;&#x5207;&#x7247;&#x5728;&#x672A;&#x521D;&#x59CB;&#x5316;&#x4E4B;&#x524D;&#x9ED8;&#x8BA4;&#x4E3A; nil&#xFF0C;&#x957F;&#x5EA6;&#x4E3A; 0&#xFF0C;&#x5B9E;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> numbers [] <span class="hljs-keyword">int</span>

    printSlice(numbers)

    <span class="hljs-keyword">if</span> numbers == <span class="hljs-literal">nil</span> {
        fmt.Println(<span class="hljs-string">&quot;Slice is empty&quot;</span>)
    }
}

<span class="hljs-keyword">func</span> printSlice(x [] <span class="hljs-keyword">int</span>) {
    fmt.Printf(<span class="hljs-string">&quot;len=%d, cap=%d, slice=%v\n&quot;</span>, <span class="hljs-built_in">len</span>(x), <span class="hljs-built_in">cap</span>(x), x)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8FD0;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;:</p>
<pre><code class="lang-go"><span class="hljs-built_in">len</span>=<span class="hljs-number">0</span>, <span class="hljs-built_in">cap</span>=<span class="hljs-number">0</span>, slice=[]
Slice is empty
</code></pre>
<hr>
<h2 id="&#x5207;&#x7247;&#x622A;&#x53D6;">&#x5207;&#x7247;&#x622A;&#x53D6;</h2>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8BBE;&#x7F6E;&#x4E0B;&#x9650;&#x53CA;&#x4E0A;&#x9650;&#x6765;&#x8BBE;&#x7F6E;&#x622A;&#x53D6;&#x5207;&#x7247; <em><code>[lower-bound:upper-bound]</code></em>&#xFF0C;&#x5B9E;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x521B;&#x5EFA;&#x5207;&#x7247; */</span>
    numbers := [] <span class="hljs-keyword">int</span> {<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>}
    printSlice(numbers)

    <span class="hljs-comment">/* &#x6253;&#x5370;&#x539F;&#x59CB;&#x5207;&#x7247; */</span>
    fmt.Println(<span class="hljs-string">&quot;numbers ==&quot;</span>, numbers)

    <span class="hljs-comment">/* &#x6253;&#x5370;&#x5B50;&#x5207;&#x7247;&#x4ECE;&#x7D22;&#x5F15;1(&#x5305;&#x542B;) &#x5230;&#x7D22;&#x5F15;4(&#x4E0D;&#x5305;&#x542B;)*/</span>
    fmt.Println(<span class="hljs-string">&quot;numbers[1:4] ==&quot;</span>, numbers[<span class="hljs-number">1</span>:<span class="hljs-number">4</span>])

    <span class="hljs-comment">/* &#x9ED8;&#x8BA4;&#x4E0B;&#x9650;&#x4E3A; 0*/</span>
    fmt.Println(<span class="hljs-string">&quot;numbers[:3] ==&quot;</span>, numbers[:<span class="hljs-number">3</span>])

    <span class="hljs-comment">/* &#x9ED8;&#x8BA4;&#x4E0A;&#x9650;&#x4E3A; len(s)*/</span>
    fmt.Println(<span class="hljs-string">&quot;numbers[4:] ==&quot;</span>, numbers[<span class="hljs-number">4</span>:])

    numbers1 := <span class="hljs-built_in">make</span>([] <span class="hljs-keyword">int</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>)
    printSlice(numbers1)

    <span class="hljs-comment">/* &#x6253;&#x5370;&#x5B50;&#x5207;&#x7247;&#x4ECE;&#x7D22;&#x5F15;  0(&#x5305;&#x542B;) &#x5230;&#x7D22;&#x5F15; 2(&#x4E0D;&#x5305;&#x542B;) */</span>
    number2 := numbers[:<span class="hljs-number">2</span>]
    printSlice(number2)

    <span class="hljs-comment">/* &#x6253;&#x5370;&#x5B50;&#x5207;&#x7247;&#x4ECE;&#x7D22;&#x5F15; 2(&#x5305;&#x542B;) &#x5230;&#x7D22;&#x5F15; 5(&#x4E0D;&#x5305;&#x542B;) */</span>
    number3 := numbers[<span class="hljs-number">2</span>:<span class="hljs-number">5</span>]
    printSlice(number3)

}

<span class="hljs-keyword">func</span> printSlice(x []<span class="hljs-keyword">int</span>){
    fmt.Printf(<span class="hljs-string">&quot;len=%d, cap=%d, slice=%v\n&quot;</span>, <span class="hljs-built_in">len</span>(x), <span class="hljs-built_in">cap</span>(x), x)
}
</code></pre>
<p>&#x6267;&#x884C;&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">len=9, <span class="hljs-built_in">cap</span>=9, slice=[0 1 2 3 4 5 6 7 8]
numbers == [0 1 2 3 4 5 6 7 8]
numbers[1:4] == [1 2 3]
numbers[:3] == [0 1 2]
numbers[4:] == [4 5 6 7 8]
len=0, <span class="hljs-built_in">cap</span>=5, slice=[]
len=2, <span class="hljs-built_in">cap</span>=9, slice=[0 1]
len=3, <span class="hljs-built_in">cap</span>=7, slice=[2 3 4]
</code></pre>
<hr>
<h2 id="append-&#x548C;-copy-&#x51FD;&#x6570;">append() &#x548C; copy() &#x51FD;&#x6570;</h2>
<p>&#x5982;&#x679C;&#x60F3;&#x589E;&#x52A0;&#x5207;&#x7247;&#x7684;&#x5BB9;&#x91CF;&#xFF0C;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x66F4;&#x5927;&#x7684;&#x5207;&#x7247;&#x5E76;&#x628A;&#x539F;&#x5206;&#x7247;&#x7684;&#x5185;&#x5BB9;&#x90FD;&#x62F7;&#x8D1D;&#x8FC7;&#x6765;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x63CF;&#x8FF0;&#x4E86;&#x4ECE;&#x62F7;&#x8D1D;&#x5207;&#x7247;&#x7684; copy &#x65B9;&#x6CD5;&#x548C;&#x5411;&#x5207;&#x7247;&#x8FFD;&#x52A0;&#x65B0;&#x5143;&#x7D20;&#x7684; append &#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> numbers [] <span class="hljs-keyword">int</span>
    printSlice(numbers)

    <span class="hljs-comment">/* &#x5141;&#x8BB8;&#x8FFD;&#x52A0;&#x7A7A;&#x5207;&#x7247; */</span>
    numbers = <span class="hljs-built_in">append</span>(numbers, <span class="hljs-number">0</span>)
    printSlice(numbers)

    <span class="hljs-comment">/* &#x5411;&#x5207;&#x7247;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5143;&#x7D20; */</span>
    numbers = <span class="hljs-built_in">append</span>(numbers, <span class="hljs-number">1</span>)
    printSlice(numbers)

    <span class="hljs-comment">/* &#x540C;&#x65F6;&#x6DFB;&#x52A0;&#x591A;&#x4E2A;&#x5143;&#x7D20; */</span>
    numbers = <span class="hljs-built_in">append</span>(numbers, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
    printSlice(numbers)

    <span class="hljs-comment">/* &#x521B;&#x5EFA;&#x5207;&#x7247; numbers1 &#x662F;&#x4E4B;&#x524D;&#x5207;&#x7247;&#x7684;&#x4E24;&#x500D;&#x5BB9;&#x91CF;*/</span>
    numbers1 := <span class="hljs-built_in">make</span>([] <span class="hljs-keyword">int</span>, <span class="hljs-built_in">len</span>(numbers), <span class="hljs-built_in">cap</span>(numbers) * <span class="hljs-number">2</span>)

    <span class="hljs-comment">/* &#x62F7;&#x8D1D; numbers &#x7684;&#x5185;&#x5BB9;&#x5230; numbers1 */</span>
    <span class="hljs-built_in">copy</span>(numbers1, numbers)
    printSlice(numbers1)
}

<span class="hljs-keyword">func</span> printSlice(x []<span class="hljs-keyword">int</span>){
    fmt.Printf(<span class="hljs-string">&quot;len=%d, cap=%d, slice=%v\n&quot;</span>, <span class="hljs-built_in">len</span>(x), <span class="hljs-built_in">cap</span>(x), x)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">len=0, <span class="hljs-built_in">cap</span>=0, slice=[]
len=1, <span class="hljs-built_in">cap</span>=1, slice=[0]
len=2, <span class="hljs-built_in">cap</span>=2, slice=[0 1]
len=5, <span class="hljs-built_in">cap</span>=6, slice=[0 1 2 3 4]
len=5, <span class="hljs-built_in">cap</span>=12, slice=[0 1 2 3 4]
</code></pre>
<hr>
<h2 id="ps">PS</h2>
<ol>
<li><p>&#x5207;&#x7247;&#x5BB9;&#x91CF;&#x7684;&#x53D8;&#x5316;</p>
<p> &#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x67E5;&#x770B;$GOROOT/src/runtime/slice.go&#x6E90;&#x7801;&#xFF0C;&#x5176;&#x4E2D;&#x6269;&#x5BB9;&#x76F8;&#x5173;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"> <span class="hljs-built_in">cap</span>
 doublecap := newcap + newcap
 <span class="hljs-keyword">if</span> <span class="hljs-built_in">cap</span> &gt; doublecap {
     newcap = <span class="hljs-built_in">cap</span>
 } <span class="hljs-keyword">else</span> {
     <span class="hljs-keyword">if</span> old.<span class="hljs-built_in">len</span> &lt; <span class="hljs-number">1024</span> {
         newcap = doublecap
     } <span class="hljs-keyword">else</span> {
         <span class="hljs-comment">// Check 0 &lt; newcap to detect overflow</span>
         <span class="hljs-comment">// and prevent an infinite loop.</span>
         <span class="hljs-keyword">for</span> <span class="hljs-number">0</span> &lt; newcap &amp;&amp; newcap &lt; <span class="hljs-built_in">cap</span> {
             newcap += newcap / <span class="hljs-number">4</span>
         }
         <span class="hljs-comment">// Set newcap to the requested cap when</span>
         <span class="hljs-comment">// the newcap calculation overflowed.</span>
         <span class="hljs-keyword">if</span> newcap &lt;= <span class="hljs-number">0</span> {
             newcap = <span class="hljs-built_in">cap</span>
         }
     }
 }
</code></pre>
<p> &#x4ECE;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;&#xFF1A;</p>
<ul>
<li>&#x9996;&#x5148;&#x5224;&#x65AD;&#xFF0C;&#x5982;&#x679C;&#x65B0;&#x7533;&#x8BF7;&#x5BB9;&#x91CF;&#xFF08;cap&#xFF09;&#x5927;&#x4E8E;2&#x500D;&#x7684;&#x65E7;&#x5BB9;&#x91CF;&#xFF08;old.cap&#xFF09;&#xFF0C;&#x6700;&#x7EC8;&#x5BB9;&#x91CF;&#xFF08;newcap&#xFF09;&#x5C31;&#x662F;&#x65B0;&#x7533;&#x8BF7;&#x7684;&#x5BB9;&#x91CF;&#xFF08;cap&#xFF09;&#xFF1B;</li>
<li>&#x5426;&#x5219;&#x5224;&#x65AD;&#xFF0C;&#x5982;&#x679C;&#x65E7;&#x5207;&#x7247;&#x7684;&#x957F;&#x5EA6;&#x5C0F;&#x4E8E;1024&#xFF0C;&#x5219;&#x6700;&#x7EC8;&#x5BB9;&#x91CF;(newcap)&#x5C31;&#x662F;&#x65E7;&#x5BB9;&#x91CF;(old.cap)&#x7684;&#x4E24;&#x500D;&#xFF0C;&#x5373;&#xFF08;newcap=doublecap&#xFF09;&#xFF1B;</li>
<li>&#x5426;&#x5219;&#x5224;&#x65AD;&#xFF0C;&#x5982;&#x679C;&#x65E7;&#x5207;&#x7247;&#x957F;&#x5EA6;&#x5927;&#x4E8E;&#x7B49;&#x4E8E;1024&#xFF0C;&#x5219;&#x6700;&#x7EC8;&#x5BB9;&#x91CF;&#xFF08;newcap&#xFF09;&#x4ECE;&#x65E7;&#x5BB9;&#x91CF;&#xFF08;old.cap&#xFF09;&#x5F00;&#x59CB;&#x5FAA;&#x73AF;&#x589E;&#x52A0;&#x539F;&#x6765;&#x7684;1/4&#xFF0C;&#x5373;&#xFF08;newcap=old.cap,for {newcap += newcap/4}&#xFF09;&#x76F4;&#x5230;&#x6700;&#x7EC8;&#x5BB9;&#x91CF;&#xFF08;newcap&#xFF09;&#x5927;&#x4E8E;&#x7B49;&#x4E8E;&#x65B0;&#x7533;&#x8BF7;&#x7684;&#x5BB9;&#x91CF;(cap)&#xFF0C;&#x5373;&#xFF08;newcap &gt;= cap&#xFF09;&#xFF1B;</li>
<li><p>&#x5982;&#x679C;&#x6700;&#x7EC8;&#x5BB9;&#x91CF;&#xFF08;cap&#xFF09;&#x8BA1;&#x7B97;&#x503C;&#x6EA2;&#x51FA;&#xFF0C;&#x5219;&#x6700;&#x7EC8;&#x5BB9;&#x91CF;&#xFF08;cap&#xFF09;&#x5C31;&#x662F;&#x65B0;&#x7533;&#x8BF7;&#x5BB9;&#x91CF;&#xFF08;cap&#xFF09;&#xFF1B;</p>
<p>&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x5207;&#x7247;&#x6269;&#x5BB9;&#x8FD8;&#x4F1A;&#x6839;&#x636E;&#x5207;&#x7247;&#x4E2D;&#x5143;&#x7D20;&#x7684;&#x7C7B;&#x578B;&#x4E0D;&#x540C;&#x800C;&#x505A;&#x4E0D;&#x540C;&#x7684;&#x5904;&#x7406;&#xFF0C;&#x6BD4;&#x5982;int&#x548C;string&#x7C7B;&#x578B;&#x7684;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x5C31;&#x4E0D;&#x4E00;&#x6837;&#x3002;</p>
</li>
</ul>
</li>
</ol>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part14/" class="navigation navigation-prev " aria-label="Previous page: Go 语言结构体">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../part16/" class="navigation navigation-next " aria-label="Next page: Go 语言范围(Range)">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 语言切片(Slice)","level":"1.16","depth":1,"next":{"title":"Go 语言范围(Range)","level":"1.17","depth":1,"path":"part16/README.md","ref":"part16/README.md","articles":[]},"previous":{"title":"Go 语言结构体","level":"1.15","depth":1,"path":"part14/README.md","ref":"part14/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part15/README.md","mtime":"2021-01-20T06:38:01.689Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

